#include <sstream>
#include <iostream>
#include <ctime>

#include <curl/curl.h>

#include "drive.h"

#define DRIVE 18

CURLcode drive(CURL *curl, std::string url, Direction dir, int speed) {
    std::stringstream command;

    command << url;
    command << "/rev.cgi?Cmd=nav&action=18&drive=";
    command << dir;
    command << "&speed=";
    command << speed;

    curl_easy_setopt(curl, CURLOPT_URL, command.str().c_str());
    return curl_easy_perform(curl);
}

long msec() {
    struct timeval time;
    gettimeofday(&time, NULL);
    return ((time.tv_sec) * 1000 + time.tv_usec/1000.0) + 0.5;
}

void drive(CURL *curl, std::string url, Direction dir, int speed, int move, int pause) {
    long end = msec() + move;

    while (msec() < end) {
        drive(curl, url, dir, speed);
    }

    end += pause;

    while (msec() < end) {}; // Pause
}
